<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: [ JRK丶Admin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/6/26 0026
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------

namespace app\admin\controller;


use app\admin\model\AttachMent;
use app\common\controller\AdminBaseController;
use Jrk\Zipdown;
use think\facade\Db;

class AttachMents extends AdminBaseController
{
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub

        $this->model=new AttachMent();

        //附件类型
        $type=$this->model->field("type")->group("type")->select()->toArray();
        $this->assign("type",$type);
    }


    /**
     * @author: Hhy <jackhhy520@qq.com>
     * @describe:上传附件
     */
    public function uploadAttachment(){
       return $this->fetch("upattachment");
    }


    /**
     * @return string
     * @throws \Exception
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/7/2 0002
     * @describe:单图选择
     */
    public function getImagesOne(){
        //$f[] = Form::frameImageOne("avatar","头像：",Route::buildUrl('admin/AttachMents/getImagesOne', array('fodder' => 'avatar')),$info['avatar'])->icon('image')->height("500px")->width('60%')->spin(0);
        $res=$this->model->GetImagesFiles(1,30);
        $this->assign('data', $res->toArray());
        $this->assign('page', $res->render());
        return $this->fetch();
    }


    /**
     * @return string
     * @throws \Exception
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @describe:可以选择多图
     */
    public function getImagesMany(){
        // $f[] = Form::frameImages('image', '多图选择',Route::buildUrl('admin/AttachMents/getImagesMany', array('fodder' => 'image')))->icon('image')->width('90%')->height('500px');
        $res=$this->model->GetImagesFiles(1,30);
        $this->assign('data', $res->toArray());
        $this->assign('page', $res->render());
        return $this->fetch();
    }


    /**
     * @return string
     * @throws \Exception
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/7/2 0002
     * @describe:单图选择
     */
    public function getFilesOne(){
        // $f[] = Form::frameImageOne("files","单附件：",Route::buildUrl('admin/AttachMents/getFilesOne', array('fodder' => 'files')))->height("500px")->width('60%')->spin(0);
        $res=$this->model->GetImagesFiles(2,30);
        $this->assign('data', $res->toArray());
        $this->assign('page', $res->render());
        return $this->fetch();
    }


    /**
     * @return string
     * @throws \Exception
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @describe:可以选择多图
     */
    public function getFilesMany(){
        // $f[] = Form::frameImageOne("files","多附件：",Route::buildUrl('admin/AttachMents/getFilesMany', array('fodder' => 'files')))->height("500px")->width('60%')->spin(0);
        $res=$this->model->GetImagesFiles(2,30);
        $this->assign('data', $res->toArray());
        $this->assign('page', $res->render());
        return $this->fetch();
    }

    /**
     * @param AttachMent $attachMent
     * @return \think\response\Json
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/7/29 0029
     * @describe:单个文件下载
     */
    public function sign_down(AttachMent $attachMent){
        $id= $this->request->param("id");
        $data=$this->model->where('id', $id)->find();
        $file_path=app()->getRootPath().'public'.$data['att_dir'];
        if(!file_exists($file_path)){
            $this->error("文件不存在");exit;
        }
        $file_name=$data['name'];
        return $attachMent->down_signal_file($file_name,$file_path);
    }


    /**
     * @author: Hhy <jackhhy520@qq.com>
     * @date: 2020/7/2 0002
     * @describe:打包下载
     */
    public function download(){
      $id= $this->request->param("id");
        if (is_array($id)){
            $ids=$id;
        }else{
            $ids=@explode(",",$id);
        }
        $data=$this->model->where('id', 'in',$ids)->select()->toArray();
        //dd($data);

        if (empty($data)) {
            $this->error("暂无数据");
        }

        $zip=new Zipdown();
        //打包下载
         $zip->zip_file($data);
    }



}